package com.irun.sm.ui.demo.ui;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.Toast;

import com.irun.sm.ui.demo.utils.WifiUtils;

/***
 * @author huangsm
 * @date 2013-7-1
 * @email huangsanm@gmail.com
 * @desc wifi连接测试
 */
public class WifiActivity extends Activity {

	// wifi
	private WifiUtils mWifi;
	private Context mContext;
	// 连接线程
	private Thread mThread;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.wifi_layout);

		mContext = this;

		mWifi = new WifiUtils(mContext);
		mWifi.openWifi();
		connection();
	}

	private void connection() {
		mThread = new Thread(new Runnable() {
			@Override
			public void run() {
				boolean result = mWifi.addNetWork(mWifi.CreateWifiInfo(
						"tianxian", "tx20120903@dongwang", 3));
				if (result) {
					mHandler.sendEmptyMessage(1);
				} else {
					mHandler.sendEmptyMessage(0);
				}
			}
		});
		mThread.start();
	}

	private Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			if (msg.what == 1) {
				Toast.makeText(mContext, "连接成功!", Toast.LENGTH_SHORT).show();
				System.out.println("stop.....");
				// 连接成功退出线程
			} else if (msg.what == 0) {
				Toast.makeText(mContext, "连接失败!", Toast.LENGTH_SHORT).show();
			}
		}
	};
}
